package com.sgcc.fj.pmos.e_fj_home.presenter;

import android.text.TextUtils;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.fj.pmos.common.base.BasePresenter;
import com.sgcc.fj.pmos.common.http.HttpInterConst;
import com.sgcc.fj.pmos.common.http.HttpRequestUtils;
import com.sgcc.fj.pmos.common.http.MvpNetCallback;
import com.sgcc.fj.pmos.common.utils.AssetsUtil;
import com.sgcc.fj.pmos.common.utils.ListPageUtil;
import com.sgcc.fj.pmos.e_fj_home.bean.MarketBean;
import com.sgcc.fj.pmos.e_fj_home.bean.MarketUserBean;
import com.sgcc.fj.pmos.e_fj_home.bean.MembersMarketBean;
import com.sgcc.fj.pmos.e_fj_home.contract.PowerUsersContract;

import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;


public class PowerUsersPresenter extends BasePresenter<PowerUsersContract.View> implements PowerUsersContract.Presenter {

    @Override
    public void getPowerUsersList(String membersName,String num, String ele, int listSize, boolean isloading) {

        Map<String, Object> params = new HashMap<>();
        params.putAll(ListPageUtil.getPageInfo(listSize));
        params.put("membersName", membersName);
        params.put("isSign", "");
        params.put("signExpiryDate", "");
        params.put("signSort", num);
        params.put("expirySort", ele);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.HOME_MARKETUSERLIST, new MvpNetCallback<MarketUserBean>(getView(), isloading) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                if (!isViewAttached()) {
                    return;
                }
                // 失败回调 判断界面是否销毁
//                getView().queryGoodsListFailed();
            }

            @Override
            public void onResponse(MarketUserBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                if (response != null) {
                    if (response.getCode()==200) {
                        getView().querySuccess(response.getData().isHasNextPage(),response.getData().getList());
                        getView().totalQuerySuccess(response.getData().getTotal());

                    }else{
                        ToastUtils.showShort(response.getMsg());
                    }
                } else {
                    ToastUtils.showShort("电力用户获取失败");
                }

            }
        });




//                String json = AssetsUtil.readStringFromAssets("membersmarket.json");
////        String json ="";
//        MembersMarketBean membersMarketBean = GsonUtils.fromJson(json, MembersMarketBean.class);
//        int pageNum = new ListPageUtil(listSize, ListPageUtil.pageSize).getTotalPage();
//
//
//        List<MembersMarketBean.PowerUserTableBean> list = null;
//        if (TextUtils.isEmpty(num) && TextUtils.isEmpty(ele)) {
//            list = membersMarketBean.getPowerUserTableList(pageNum, membersMarketBean.getPowerUserTable());
//        } else if (!TextUtils.isEmpty(num) && "1".equals(num)) {
//            list = membersMarketBean.getPowerUserTableList(pageNum, SortNum(membersMarketBean.getPowerUserTable(), true));
//        } else if (!TextUtils.isEmpty(num) && "2".equals(num)) {
//            list = membersMarketBean.getPowerUserTableList(pageNum, SortNum(membersMarketBean.getPowerUserTable(), false));
//        } else if (!TextUtils.isEmpty(ele) && "1".equals(ele)) {
//            list = membersMarketBean.getPowerUserTableDownList(pageNum);
//        } else if (!TextUtils.isEmpty(ele) && "2".equals(ele)) {
//            list = membersMarketBean.getPowerUserTableUPList(pageNum);
//        } else {
//            list = membersMarketBean.getPowerUserTableList(pageNum, membersMarketBean.getPowerUserTable());
//        }
//        boolean isHasNextPage = ListPageUtil.isHasNextPage(membersMarketBean.getPowerUserTable().size(), pageNum);
//        getView().querySuccess(isHasNextPage, list);
    }


    /**
     * @param List 要排序的数组
     * @param type 是否要降序排
     */
    public List<MembersMarketBean.PowerUserTableBean> SortNum(List<MembersMarketBean.PowerUserTableBean> List, boolean type) {
        //排序
        Collections.sort(List, new Comparator<MembersMarketBean.PowerUserTableBean>() {
            @Override
            public int compare(MembersMarketBean.PowerUserTableBean o1, MembersMarketBean.PowerUserTableBean o2) {

                Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
                return com.compare(o1.getIsmembers(), o2.getIsmembers());

            }
        });
        //降序
        if (type) {
            Collections.reverse(List);
        }
        return List;
    }


}
